package com.elinkway.infinitemovies.http.requesttask;

import android.content.Context;

import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.bean.PlayRecord;
import com.elinkway.infinitemovies.bean.PlayRecordList;
import com.elinkway.infinitemovies.dao.PlayRecordDao;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.UserPlayRecordParser;
import com.elinkway.infinitemovies.http.requestInterface.RequestCommonInterface;
import com.lvideo.http.bean.LVideoDataHull;

import java.util.List;

/**
 * Author: liuxiaojiang(liuxiaojiang@lvideo.com)
 * Date: 2016-03-17
 * Time: 17:04
 * Description: FIXME
 */
public class RequestUserPlayRecordsTask extends
        MoviesHttpAsyncTask<PlayRecordList> {

    private int index = 0;
    private int limit = 0;
    private RequestCommonInterface<PlayRecordList> mRequestCommonInterface;

    @Override
    public void netNull() {
        super.netNull();
        if(mRequestCommonInterface != null){
            mRequestCommonInterface.netError(MoviesHttpApi.USER_PLAY_LIST);
        }
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        super.netErr(updateId, errMsg);
        if(mRequestCommonInterface != null){
            mRequestCommonInterface.netError(MoviesHttpApi.USER_PLAY_LIST);
        }
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        super.dataNull(updateId, errMsg);
        if(mRequestCommonInterface != null){
            mRequestCommonInterface.requestFail(MoviesHttpApi.USER_PLAY_LIST);
        }
    }

    public void setRequestListener(RequestCommonInterface<PlayRecordList> requestCommonInterface) {
        this.mRequestCommonInterface = requestCommonInterface;
    }

    public RequestUserPlayRecordsTask(Context context, int index, int limit) {
        super(context);
        this.index = index;
        this.limit = limit;
    }

    @Override
    public LVideoDataHull<PlayRecordList> doInBackground() {
        LVideoDataHull<PlayRecordList> ret = MoviesHttpApi.userPlayRecordRequest(
                new UserPlayRecordParser(), index, limit);
        return ret;
    }

    @Override
    public void onPostExecute(int updateId, PlayRecordList result) {
        if(result != null){
            List<PlayRecord> list = result.getPlayRecordList();
            if(list != null && list.size() > 0){
                new PlayRecordDao(context).save(list);
            }
            if(mRequestCommonInterface != null){
                mRequestCommonInterface.requestSuccess(updateId, result, MoviesHttpApi.USER_PLAY_LIST);
            }
        }else{

        }
    }
}